#include <pthread.h>
#include <assert.h>

#include "bcti_Mutex.h"
#include "easyLog.h"

namespace lzpLib{
Mutex::Mutex(){
    pthread_mutexattr_t attr;
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
    pthread_mutex_init(&lock_, &attr);
    //DEBUG_("Mutex start %p.\n",&lock_);
}

Mutex::~Mutex(){
	//DEBUG_("~Mutex start.\n");
    pthread_mutex_destroy(&lock_);
    //DEBUG_("~Mutex end.\n");
}

void Mutex::lock(){
    int ret = pthread_mutex_lock(&lock_);
    if(ret != 0)
    {
    	ERR_("pthread_mutex_lock fail %d\n", ret);
    }
//    assert(ret == 0);
}

bool Mutex::trylock(){
    int ret = pthread_mutex_trylock(&lock_);
    return ret == 0;
}

void Mutex::unlock(){
    int ret = pthread_mutex_unlock(&lock_);
    if(ret != 0)
	{
		ERR_("pthread_mutex_unlock fail %d\n", ret);
	}
    assert(ret == 0);
}
}

